/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin			*/
/*	Project:	GlbCloud			*/
/*	Author:		gong_libin			*/
/*	Date:		2012_01_09			*/
/*	File:		MrThread.cpp			*/
/********************************************************/

#include "MrThread.h"

CMrThread::CMrThread()
{
	m_iID = 0;
	m_pStartRoutine = NULL;
	memset(&m_stAttr, '\0', sizeof(pthread_attr_t));
}

CMrThread::~CMrThread()
{
}

int CMrThread::MrThreadJoin(void** ppReturn)
{
	if (MR_SUCCESS != pthread_join(m_iID, ppReturn)) {
		MR_ERROR("%s\n", strerror(errno));
		return MR_FAILURE;
	}

	return MR_SUCCESS;
}

int CMrThread::MrThreadDestroy()
{
	if (MR_SUCCESS != pthread_cancel(m_iID)) {
		MR_ERROR("%s\n", strerror(errno));
		return MR_FAILURE;
	}

	return MR_SUCCESS;
}

int CMrThread::MrThreadAttrInit()
{
	if (MR_SUCCESS != pthread_attr_init(&m_stAttr)) {
		MR_ERROR("%s\n", strerror(errno));
		return MR_FAILURE;
	}

	return MR_SUCCESS;
}

void CMrThread::MrThreadAttrSet(void* pAttr)
{
	memcpy((void*)&m_stAttr, pAttr, sizeof(pthread_attr_t));

	return;
}

int CMrThread::MrThreadAttrSetDetach()
{
	if (MR_SUCCESS != pthread_attr_setdetachstate(&m_stAttr, PTHREAD_CREATE_DETACHED)) {
		MR_ERROR("%s\n", strerror(errno));
		return MR_FAILURE;
	}

	return MR_SUCCESS;
}

int CMrThread::MrThreadCreate(void* pPara)
{
	if (MR_FAILURE == pthread_create(&m_iID, &m_stAttr, m_pStartRoutine, pPara)) {
		MR_ERROR("%s\n", strerror(errno));
		return MR_FAILURE;
	}

	return MR_SUCCESS;
}

void CMrThread::MrThreadSetStartRoutine(void* (*pStartRoutine)(void*))
{
	m_pStartRoutine = pStartRoutine;

	return;
}
